#include "setupserial.h"
#include "ui_setupserial.h"
#include "serialinfo.h"
#include <QDebug>
SetupSerial::SetupSerial(Serialworks *work,QString com,int bitrate,int parity, QDialog *parent) :
    m_works(work),
    _com(com),
    _bitrate(bitrate),
    _parity(parity),
    QDialog(parent),
    ui(new Ui::SetupSerial)
{
    ui->setupUi(this);
    setWindowTitle("Setup");
    setWindowFlags(Qt::WindowCloseButtonHint | Qt::Drawer);

    _isChanged = false;

    SerialInfo info;
    ui->comboBox->insertItems(0,info.comList());
    ui->comboBox->setCurrentText(_com);
    ui->comboBox_2->setCurrentText(QString::number(_bitrate));
    ui->comboBox_5->setCurrentIndex(_parity);

    connect(this,SIGNAL(signal_com_param(QString,int,int)),m_works,SLOT(slot_com_param(QString,int,int)));

}

SetupSerial::~SetupSerial()
{
    delete ui;
}

void SetupSerial::on_btnOk_clicked()
{
    if(ui->comboBox->currentText()!=_com){
        _com = ui->comboBox->currentText();
        _isChanged = true;
    }
    if(ui->comboBox_2->currentText().toInt()!= _bitrate){
        _bitrate = ui->comboBox_2->currentText().toInt();
        _isChanged = true;
    }
    if(ui->comboBox_5->currentIndex()!= _parity){
        _parity = ui->comboBox_5->currentIndex();
        _isChanged = true;
    }

    if(_isChanged){
        emit signal_com_param(_com,_bitrate,_parity);
    }
    accept();

}

void SetupSerial::on_btnCancel_clicked()
{
    reject();
}
